package cn.iocoder.yudao.module.forward.service.license;

import cn.hutool.crypto.SecureUtil;
import cn.iocoder.yudao.module.forward.controller.app.register.vo.AltertInfoReqVO;
import cn.iocoder.yudao.module.forward.controller.app.register.vo.RegisterReqVO;
import cn.iocoder.yudao.module.forward.controller.app.register.vo.SuspendReqVO;
import cn.iocoder.yudao.module.forward.controller.app.scope.vo.AuthorizeReqVO;
import cn.iocoder.yudao.module.forward.controller.app.scope.vo.CancelAuthReqVO;
import cn.iocoder.yudao.module.forward.utils.ShortUniqueStringGenerator;
import cn.iocoder.yudao.module.forward.utils.SnowflakeIdGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.forward.controller.admin.license.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.license.LicenseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.forward.dal.mysql.license.LicenseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.forward.enums.ErrorCodeConstants.*;

/**
 * 授权码 Service 实现类
 *
 * @author zhang
 */
@Service
@Validated
public class LicenseServiceImpl implements LicenseService {

    @Resource
    private LicenseMapper licenseMapper;

    @Override
    public Integer createLicense(LicenseSaveReqVO createReqVO) {
        // 插入
        LicenseDO license = BeanUtils.toBean(createReqVO, LicenseDO.class);
        license.setLicense("LN"+SnowflakeIdGenerator.nextId());
        license.setClientId(SecureUtil.md5(license.getLicense()));
        license.setUname(ShortUniqueStringGenerator.generate());
        license.setPwd(ShortUniqueStringGenerator.generate());
        licenseMapper.insert(license);
        // 返回
        return license.getId();
    }

    @Override
    public void updateLicense(LicenseSaveReqVO updateReqVO) {
        // 校验存在
        validateLicenseExists(updateReqVO.getId());
        // 更新
        LicenseDO updateObj = BeanUtils.toBean(updateReqVO, LicenseDO.class);
        licenseMapper.updateById(updateObj);
    }

    @Override
    public void deleteLicense(Integer id) {
        // 校验存在
        validateLicenseExists(id);
        // 删除
        licenseMapper.deleteById(id);
    }

    private void validateLicenseExists(Integer id) {
        if (licenseMapper.selectById(id) == null) {
            throw exception(LICENSE_NOT_EXISTS);
        }
    }

    @Override
    public LicenseDO getLicense(Integer id) {
        return licenseMapper.selectById(id);
    }

    @Override
    public LicenseDO getLicenseRegister(RegisterReqVO registerReqVO) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("license",registerReqVO.getLicense());
        qw.eq("owner",registerReqVO.getOwner());
        qw.eq("type_code",registerReqVO.getType());
        return licenseMapper.selectOne(qw);
    }

    @Override
    public PageResult<LicenseDO> getLicensePage(LicensePageReqVO pageReqVO) {
        return licenseMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateById(LicenseDO licenseDO) {
        licenseMapper.updateById(licenseDO);
    }

    @Override
    public LicenseDO getLicenseRegister(AltertInfoReqVO altertInfoReqVO) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("client_id",altertInfoReqVO.getClientId());
        qw.eq("owner",altertInfoReqVO.getOwner());
        qw.eq("type_code",altertInfoReqVO.getType());
        qw.eq("uname",altertInfoReqVO.getUname());
        qw.eq("pwd",altertInfoReqVO.getPwd());
        return licenseMapper.selectOne(qw);
    }

    @Override
    public LicenseDO getLicenseRegister(SuspendReqVO suspendReqVO) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("client_id",suspendReqVO.getClientId());
        qw.eq("uname",suspendReqVO.getUname());
        qw.eq("pwd",suspendReqVO.getPwd());
        return licenseMapper.selectOne(qw);
    }

    @Override
    public LicenseDO getLicenseRegister(AuthorizeReqVO authorizeReqVO) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("client_id",authorizeReqVO.getClientId());
        qw.eq("uname",authorizeReqVO.getUname());
        qw.eq("pwd",authorizeReqVO.getPwd());
        return licenseMapper.selectOne(qw);
    }

    @Override
    public LicenseDO getLicenseRegister(CancelAuthReqVO cancelAuthReqVO) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("client_id",cancelAuthReqVO.getClientId());
        qw.eq("uname",cancelAuthReqVO.getUname());
        qw.eq("pwd",cancelAuthReqVO.getPwd());
        return licenseMapper.selectOne(qw);
    }


}